<?php
class v1_sort{
    function index(){
    	$sort=$this->yum->model('sort');
    	$data['total']=$sort->Count(array('where'=>array('locked !='=>10,'label NOT IN'=>array('china','browser','format','language','modality','system'))));
    	$data['total']=empty($data['total'])?0:$data['total'][0]['count'];
		$data['sort']=$sort->GSort(array('where'=>array('locked !='=>10,'label NOT IN'=>array('china','browser','format','language','modality','system')),'limit'=>array(intval($this->yum->get('start')),15),'order'=>array('label'=>'desc','sequence'=>'desc')));
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$this->yum->config['Manage']['domain'].'index.php?c=sort&a=index','total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('index.php');
    }
    function append(){
    	$sort=$this->yum->model('sort');
    	if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$label=$this->yum->post('label',true);
			$name=$this->yum->post('name',true);
			$value=$this->yum->post('value',true);
			$parent=intval($this->yum->post('parent',true));
			$sequence=intval($this->yum->post('sequence',true));
			$locked=intval($this->yum->post('locked',true));
			if($label==''){
				$output['msg']='请选择分类目录.';
			}elseif(strlen($name)<=0){
				$output['msg']='名称不可为空.';
			}else{
				$temp=$sort->GSort(array('where'=>array('label='=>$label,'name='=>$name,'locked!='=>10)));
				if(empty($temp)){
					$affected=$sort->ISort(array('label'=>$label,'name'=>$name,'value'=>$value,'parent'=>$parent,'sequence'=>$sequence,'locked'=>$locked));
					if($affected==1){
						$output['result']=2;
						$output['msg']='添加成功.';
					}else{
						$output['msg']='您输入的数据有误.';
					}
				}else{
					$output['msg']='您输入的名称已存在.';
				}
			}
			header('Content-type:application/json');
			echo json_encode($output);
    	}else{
	    	$data['label']=$sort->GSort(array('group'=>array('label')));
	    	$this->yum->set($data);
			$this->yum->view('append.php');
    	}
	}
	function edit(){
		$sort=$this->yum->model('sort');
		if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$id=intval($this->yum->post('sort',true));$name=$this->yum->post('name',true);
			$value=$this->yum->post('value',true);$parent=intval($this->yum->post('parent',true));
			$sequence=intval($this->yum->post('sequence',true));$locked=intval($this->yum->post('locked',true));
			$affected=$sort->USort(array('where'=>array('id='=>$id),'set'=>array('name='=>$name,'value='=>$value,'parent='=>$parent,'sequence='=>$sequence,'locked='=>$locked)));
			if($affected==1){
				$output['result']=2;
				$output['msg']='修改成功.';
			}else{
				$output['msg']='请误提交相同或未经修改的数据.';
			}
			header('Content-type:application/json');
			echo json_encode($output);
		}else{
			$data['sort']=$sort->GSort(array('where'=>array('id ='=>intval($this->yum->get('sort',true)))));
			$data['sort']=reset($data['sort']);
			$data['label']=$sort->GSort(array('group'=>array('label')));
			$this->yum->set($data);
			$this->yum->view('edit.php');
		}
	}
	function lock(){
		$output=array('result'=>0,'msg'=>'');
		$sort=$this->yum->model('sort');
		$id=$this->yum->get('sort');
		$affected=$sort->USort(array('where'=>array('id='=>$id),'set'=>array('locked='=>intval($this->yum->get('locked',true)))));
		if($affected==1){$output['result']=1;}else{$output['msg']='请求有误.';}
		header('Content-Type:text/plain');
		echo json_encode($output);
	}
}
?>